<%@ Page Title="" Language="C#" MasterPageFile="~/Themes/Default/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="MvcContrib.UI.Html" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	<%=Html.Resource("Role", "EDITROLE")%>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <form id="form1" action="/Role/Edit" method="post">
    <h2><%=Html.Resource("Role", "EDITROLE")%></h2>
    <% =Html.Hidden("roleid",ViewData["roleid"]) %>
    <div class="tabcontainer">
    <input type="submit" value="<% =Html.Resource("Global","SAVE") %>" name="btnSave" class="button" accesskey="S"/>
    <input type="button" value="<% =Html.Resource("Global","CLEAR") %>" name="btnClear" class="button" accesskey="C"/>
    <input type="button" value="<% =Html.Resource("Global","CANCEL") %>" name="btnCancel" class="button" accesskey="X" onclick="redirectToAction('Role','Index')"/>
    </div>
    <% =Html.RenderForm(ViewData.Model)%>
    <div class="tabcontainer">
    <input type="submit" value="<% =Html.Resource("Global","SAVE") %>" name="btnSave" class="button" accesskey="S"/>
    <input type="button" value="<% =Html.Resource("Global","CLEAR") %>" name="btnClear" class="button" accesskey="C"/>
    <input type="button" value="<% =Html.Resource("Global","CANCEL") %>" name="btnCancel" class="button" accesskey="X" onclick="redirectToAction('Role','Index')"/>
    </div>
    
    <div class="tabcontainer">
    <table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
    <div id="pager" class="scroll" style="text-align:center;"></div>
    </div>
    </form>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
<%=Html.jQueryValidateScriptInclude() %>
        <%=Html.Stylesheet("~/Content/jgGrid/ui.jqgrid.css")%>
	    <%=Html.ScriptInclude("~/Scripts/jgGrid/i18n/grid.locale-"+Html.GetjqGridCulture()+".js")%>
        <%=Html.ScriptInclude("~/Scripts/jgGrid/jquery.jqGrid.min.js")%>
</asp:Content>

<asp:Content ID="Content4" ContentPlaceHolderID="PostScript" runat="server">
<script type="text/javascript">
    $(document).ready(function() {
        jQuery("#list").jqGrid({
            url: '/Role/GetUsersByRoleId/?roleid=<% =ViewData["roleid"] %>',
            datatype: 'json',
            mtype: 'GET',
            colNames: ['ID', '<% =Html.Resource("User","username") %>', '<% =Html.Resource("User","Email") %>', '<% =Html.Resource("Global","ACTION") %>'],
            colModel: [
          { name: 'id', index: 'id', hidden: true },
          { name: 'username', index: 'username', width: 100, align: 'left' },
          { name: 'email', index: 'email', width: 100, align: 'left' },
          {name:'act',index:'act', width:36,sortable:false,align: 'center'}],
            sortname: 'username',
            sortorder: "asc",
            toolbar:[true,"top"],
            loadComplete: function() {
                var ids = jQuery("#list").getDataIDs();
                for (var i = 0; i < ids.length; i++) {
                    var cl = ids[i];
                    be = "<div align='center'><%=Html.RenderjQueryIcon("deletebtn","ui-icon-circle-close","Remove from this role",null,"rowid='\"+cl+\"'") %></div>";
                    jQuery("#list").setRowData(ids[i], { act: be });
                    jQuery(".deletebtn").click(deleteButtonClick);
                }
            },
            viewrecords: true,
            imgpath: '/scripts/themes/coffee/images',
            caption: '<%=Html.Resource("Role","USERINROLE") %>',
            hidegrid: false,
            width: 500,
            height: 200,
            multiselect: false,
            editurl: '/User/Edit/'
        });
        jQuery("#t_list").append("<input id='toolbar_addbutton' type='button' class='button ui-state-default ui-corner-all' value='<%=Html.Resource("Role","ADDUSERTOROLE")%>'/>");
        jQuery("#toolbar_addbutton").click(function()
        {
            open_popup("/User/Popup/",550,500,true,true,{
            "call_back_function":"set_return_and_save_background",
            "form_name":"form1",
            "field_to_name_array":{"id":"subpanel_id"},
            "passthru_data":{
	            "child_field":"users",
	            "link_field_name":"users",
	            "module_name":"user",
	            "return_url":"/Role/AddUsersToRole?roleid=<%=ViewData["roleid"] %>",
	            "refresh_page":"0"}},
	        "multiselect");
        });
        
        $("input[name='btnClear']").click(function() { clearFormFields("#form1"); });
    });
    function deleteButtonClick()
    {
        var rowid=$(this).attr("rowid");
        if(rowid!=null)
        removeUser(rowid);
    }
    //render function for /user/popup data
    function renderData(data, textStatus)
    {
        var rows = eval(data);
        for (var i = 0; i < rows.length; i++) {
            var rowdata = rows[i];
            rowdata.cell.act="<div align='center'><%=Html.RenderjQueryIcon("deletebtn","ui-icon-circle-close","Remove from this role",null,"rowid='\"+rowdata.id+\"'") %></div>";
            jQuery("#list").addRowData(rowdata.id, rowdata.cell);
            jQuery("#"+rowdata.id+" .deletebtn").click(deleteButtonClick);
        }
    }
    
    function removeUser(rowid) {
        var rowdata = jQuery("#list").getRowData(rowid);
        
        jQuery("#list").delGridRow(rowid,{
            modal:false,
            caption:'<% =Html.Resource("Global","CONFIRMDIALOG") %>',
            msg:'<% =Html.Resource("Role","DELETEUSERMESSAGE") %>',
            bSubmit:'<% =Html.Resource("Global","YES") %>',
            bCancel:'<% =Html.Resource("Global","NO") %>',
            reloadAfterSubmit:false,
            url:'/Role/RemoveUserFromRole/?userid='+rowdata.id+'&roleid=<%=ViewData["roleid"] %>'
        });
    }
</script>
</asp:Content>

